package com.cgnpc.scp.run.utils;

import com.cgnpc.cuddemo.utils.IDHelper;
import com.cgnpc.framework.userinfo.CurrentUser;
import com.cgnpc.scp.common.pojo.UserInfo;
import com.cgnpc.scp.common.utils.StringUtil;
import com.cgnpc.scp.run.mybatis.entity.BaseEntity;

import java.util.Date;

public class CommonRunUtil {

    public static boolean isIdNullOrSpace(BaseEntity obj){
        return StringUtil.isNullOrSpace(obj.getId());
    }

    public static BaseEntity saveOrUpdateData(BaseEntity obj, CurrentUser currentUser){
        UserInfo currentUserDetail = currentUser.getCurrentUserDetail();
        Date date = new Date();
        if(isIdNullOrSpace(obj)){
            //Id为空,需要保存数据
            obj.setCreateUser(currentUserDetail.getUserId());
            obj.setCreateUserName(currentUserDetail.getUserName());
            obj.setCreateTime(date);
            obj.setLastUpdateTime(date);
            obj.setId(IDHelper.uuid());
        }else{
            //Id不为空,更新数据
            obj.setLastUpdateUser(currentUserDetail.getUserId());
            obj.setLastUpdateUserName(currentUserDetail.getUserName());
            obj.setLastUpdateTime(date);
        }
        return obj;
    }

}
